#define _CRT_SECURE_NO_WARNINGS 1
#include "contact.h"
//菜单
void menu()
{
	printf("***********************************************\n");
	printf("************1.Add         2.Del****************\n");
	printf("************3.Search      4.Modify*************\n");
	printf("************5.Display     6.Empty**************\n");
	printf("************7.Sort_name   0.Exit***************\n");
	printf("***********************************************\n");
}


//打印标题
void Print_head()
{
	char* emtpy = " ";
	printf("姓名%-21s性别%-3s年龄%-2s电话%-11s地址%-31s\n", emtpy, emtpy, emtpy, emtpy, emtpy);
}


//动态初始化通讯录
void Init_Con(Con* ps)
{
	assert(ps);
	ps->sz = 0;
	ps->capacity = INITIAL_MAN;
	ps->data = (Message*)malloc(ps->capacity * sizeof(Message));
	if (ps->data == NULL)
	{
		perror("Init_Con::malloc");
		return;
	}
	memset(ps->data, 0, ps->capacity * sizeof(Message));
}


//扩容
void Expasion_capacity(Con* ps)
{
	Message* pa = (Message*)realloc(ps->data, (ps->capacity + 5)*sizeof(Message));
	if (pa != NULL)
	{
		ps->data = pa;
	}
	else
	{
		perror("Expasion_capacity::realloc");
		return;
	}
	//memset(ps->data + ps->capacity, 0, 5 * sizeof(Message));
	ps->capacity += 5;
	printf("扩容成功\n");
}



//添加联系人
void Add_message(Con* ps)
{
	assert(ps);
	if (ps->sz == ps->capacity)
	{
		Expasion_capacity(ps);
	}
	printf("输入联系人姓名>");
	scanf("%s", ps->data[ps->sz].name);
	printf("输入联系人性别>");
	scanf("%s", ps->data[ps->sz].sex);
	printf("输入联系人年龄>");
	scanf("%d", &ps->data[ps->sz].age);
	printf("输入联系人电话>");
	scanf("%s", ps->data[ps->sz].tel);
	printf("输入联系人地址>");
	scanf("%s", ps->data[ps->sz].address);
	ps->sz++;
}


//显示通讯录
void Display_book(const Con* ps)
{
	assert(ps);
	Print_head();
	for (int i = 0; i < ps->sz; i++)
	{
		printf("%-25s%-7s%-6d%-15s%-35s\n", ps->data[i].name, ps->data[i].sex, ps->data[i].age, ps->data[i].tel, ps->data[i].address);
	}
	printf("\n");
}


//删除联系人
void Delete_message(Con* ps)
{
	assert(ps);
	printf("输入待删除联系人姓名>");
	char arr[20] = { 0 };
	scanf("%s", arr);
	int i = 0;
	for (i = 0; i < ps->sz; i++)
	{
		if (0 == strcmp(arr, ps->data[i].name))
		{
			break;
		}
	}
	for (i; i < ps->sz - 1; i++)
	{
		ps->data[i] = ps->data[i + 1];
	}
	if (ps->sz == 0 || i == ps->sz)
	{
		printf("通讯录内无联系人，无法执行删除\n");
		return;
	}
	ps->sz--;
	printf("删除联系人成功\n");
}


//查找联系人
int Search_message(const Con* ps)
{
	assert(ps);
	if (ps->sz == 0)
	{
		printf("通讯录内无联系人，无法执行查找\n");
		return -1;
	}
	printf("输入待查找联系人姓名>");
	char arr[20] = { 0 };
	scanf("%s", arr);
	int i = 0;
	for (i = 0; i < ps->sz; i++)
	{
		if (0 == strcmp(arr, ps->data[i].name))
		{
			Print_head();
			printf("%-25s%-7s%-6d%-15s%-35s\n", ps->data[i].name, ps->data[i].sex, ps->data[i].age, ps->data[i].tel, ps->data[i].address);
			return i;
		}
	}
	printf("通讯录无此人\n");
	return -1;
}


//修改选项
void Modify_menu()
{
	printf("============\n");
	printf("===1.性别===\n");
	printf("===2.年龄===\n");
	printf("===3.电话===\n");
	printf("===4.地址===\n");
	printf("===0.退出===\n");
	printf("============\n");
}
//修改联系人性别
void modify_sex(Con* ps, int index)
{
	assert(ps);
	char str[SEX] = { 0 };
	printf("修改为>");
	scanf("%s", str);
	strcpy(ps->data[index].sex, str);
	printf("修改成功\n");
}
//修改联系人年龄
void modify_age(Con* ps, int index)
{
	assert(ps);
	int age = 0;
	printf("修改为>");
	scanf("%d", &age);
	ps->data[index].age = age;
	printf("修改成功\n");
}
//修改联系人电话
void modify_tel(Con* ps, int index)
{
	assert(ps);
	char str[TEL] = { 0 };
	printf("修改为>");
	scanf("%s", str);
	strcpy(ps->data[index].tel, str);
	printf("修改成功\n");
}
//修改联系人地址
void modify_address(Con* ps, int index)
{
	assert(ps);
	char str[ADDRESS] = { 0 };
	printf("修改为>");
	scanf("%s", str);
	strcpy(ps->data[index].address, str);
	printf("修改成功\n");
}
//修改联系人
void Modify_message(Con* ps)
{
	assert(ps);
	int index = Search_message(ps);
	Modify_menu();
	int num = 0;
	printf("请选择修改项>");
	do {
		scanf("%d", &num);
		switch (num)
		{
		case 0:
			break;
		case 1:
			modify_sex(ps, index);
			break;
		case 2:
			modify_age(ps, index);
			break;
		case 3:
			modify_tel(ps, index);
			break;
		case 4:
			modify_address(ps, index);
			break;
		default:
			printf("选择错误\n");
			break;
		}
	} while (num);

}


//清空联系人
void Empty_book(Con* ps)
{
	assert(ps);
	Init_Con(ps);
	printf("通讯录已清空\n");
}


//比较函数
int cmp(const void* elm1, const void* elm2)
{
	return (strcmp(((Message*)elm1)->name, ((Message*)elm2)->name));
}

//按姓名排序
Con* sort_name(Con* ps)
{
	assert(ps);
	qsort(ps->data, ps->sz, sizeof(Message), cmp);
	printf("排序完成\n");
	return ps;
}

//销毁通讯录
void Release_con(Con* ps)
{
	free(ps->data);
	ps->data = NULL;
	ps->sz = 0;
	ps->capacity = 0;
}